package com.example.edu.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.edu.dao.DependencyDao;
import com.example.edu.domain.Dependency;
import com.example.edu.domain.query.DependencyQuery;
import com.example.edu.domain.vo.DependencyVO;
import com.example.edu.domain.vo.PageVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author yanzhao
 * @date 2022/1/15 18:56
 * @since 1.0.0
 */
@Service
public class DependencyService extends ServiceImpl<DependencyDao, Dependency> {

    @Resource
    private DependencyDao dependencyDao;

    public PageVO<DependencyVO> getPage(DependencyQuery query) {

        Page<Dependency> page = new Page(query.getPageNum(), query.getPageSize());

        QueryWrapper<Dependency> queryWrapper = getQueryWrapper(query);
        List<DependencyVO> dependencyVOS = new ArrayList<>();

        Page<Dependency> StarterDepPage = dependencyDao.selectPage(page, queryWrapper);
        List<Dependency> records = StarterDepPage.getRecords();
        if (records != null && !records.isEmpty()) {
            dependencyVOS = covert(records);
        }
        return PageVO.getInstance(query.getPageNum(), query.getPageSize(), dependencyVOS, (int) StarterDepPage.getTotal());
    }

    private QueryWrapper<Dependency> getQueryWrapper(DependencyQuery query) {
        QueryWrapper<Dependency> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(query.getGroupId())) {
            queryWrapper.eq("group_id", StringUtils.trim(query.getGroupId()));
        }
        if (StringUtils.isNotBlank(query.getArtifactId())) {
            queryWrapper.like("artifact_id", StringUtils.trim(query.getArtifactId()));
        }
        queryWrapper.select("id", "group_id", "artifact_id", "version", "scope", "optional", "gmt_create", "gmt_modified");
        queryWrapper.orderByDesc("gmt_modified", "gmt_create");
        return queryWrapper;
    }

    private List<DependencyVO> covert(List<Dependency> dependencies) {
        List<DependencyVO> dependencyVOS = new ArrayList<>();
        if (dependencies == null || dependencies.isEmpty()) {
            return dependencyVOS;
        }
        DependencyVO depVO = null;
        for (Dependency record : dependencies) {
            depVO = new DependencyVO();
            depVO.setGroupId(record.getGroupId());
            depVO.setArtifactId(record.getArtifactId());
            depVO.setVersion(record.getVersion());
            depVO.setScope(record.getScope());
            depVO.setOptional(record.getOptional());
            depVO.setGmtCreate(record.getGmtCreate());
            depVO.setGmtModified(record.getGmtModified());
            depVO.setId(record.getId() + "");
            dependencyVOS.add(depVO);
        }
        return dependencyVOS;
    }
}
